package com.neusoft.controller;


import java.util.List;

import com.neusoft.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.neusoft.po.Cart;
import com.neusoft.po.CommonResult;
import com.neusoft.service.CartService;

//@CrossOrigin("*")
@RestController
@RequestMapping("/CartController")
@RefreshScope //开启动态刷新
public class CartController {

	@Autowired
	private CartService cartService;


	@GetMapping("/listCart/{tokenString}")
	public CommonResult<List> listCart(@PathVariable("tokenString") String tokenString) throws Exception {
		try {
			TokenUtil.verify(tokenString);
		}catch(Exception e){
			return new CommonResult(401, "Unauthorized", 0);
		}
		if(TokenUtil.verify(tokenString)) {
			String userId = TokenUtil.tokenUser(tokenString);
			Cart param = new Cart();
			param.setUserId(userId);
			List<Cart> list = cartService.listCart(param);
			return new CommonResult(200, "success", list);
		}else {
			return new CommonResult(401, "Unauthorized", 0);
		}

	}

	@GetMapping("/listCart/{tokenString}/{businessId}")
	public CommonResult<List> listCart(@PathVariable("tokenString") String tokenString,
									   @PathVariable("businessId") Integer businessId) throws Exception {

		try {
			TokenUtil.verify(tokenString);
		}catch(Exception e){
			return new CommonResult(401, "Unauthorized", 0);
		}
		if(TokenUtil.verify(tokenString)) {
			String userId = TokenUtil.tokenUser(tokenString);
			Cart param = new Cart();
			param.setUserId(userId);
			param.setBusinessId(businessId);
			List<Cart> list = cartService.listCart(param);
			return new CommonResult(200, "success", list);
		}else {
			return new CommonResult(401, "Unauthorized", 0);
		}

	}

	@PostMapping("/saveCart/{tokenString}/{businessId}/{foodId}")
	public CommonResult<Integer> saveCart(@PathVariable("tokenString") String tokenString,
										  @PathVariable("businessId") Integer businessId, @PathVariable("foodId") Integer foodId) throws Exception {
		try {
			TokenUtil.verify(tokenString);
		}catch(Exception e){
			return new CommonResult(401, "Unauthorized", 0);
		}
		if(TokenUtil.verify(tokenString)) {
			String userId = TokenUtil.tokenUser(tokenString);
			Cart param = new Cart();
			param.setUserId(userId);
			param.setBusinessId(businessId);
			param.setFoodId(foodId);
			int result = cartService.saveCart(param);
			return new CommonResult(200, "success", result);
		}else {
			return new CommonResult(401, "Unauthorized", 0);
		}

	}

	@PutMapping("/updateCart/{tokenString}/{businessId}/{foodId}/{quantity}")
	public CommonResult<Integer> updateCart(@PathVariable("tokenString") String tokenString,
											@PathVariable("businessId") Integer businessId, @PathVariable("foodId") Integer foodId,
											@PathVariable("quantity") Integer quantity) throws Exception {
		try {
			TokenUtil.verify(tokenString);
		}catch(Exception e){
			return new CommonResult(401, "Unauthorized", 0);
		}
		if(TokenUtil.verify(tokenString)) {
			String userId = TokenUtil.tokenUser(tokenString);
			Cart param = new Cart();
			param.setUserId(userId);
			param.setBusinessId(businessId);
			param.setFoodId(foodId);
			param.setQuantity(quantity);
			int result = cartService.updateCart(param);
			return new CommonResult(200, "success", result);
		}else {
			return new CommonResult(401, "Unauthorized", 0);
		}
	}

	@DeleteMapping("/removeCart/{tokenString}/{businessId}/{foodId}")
	public CommonResult<Integer> removeCart(@PathVariable("tokenString") String tokenString,
											@PathVariable("businessId") Integer businessId, @PathVariable("foodId") Integer foodId) throws Exception {
		try {
			TokenUtil.verify(tokenString);
		}catch(Exception e){
			return new CommonResult(401, "Unauthorized", 0);
		}
		if(TokenUtil.verify(tokenString)) {
			String userId = TokenUtil.tokenUser(tokenString);
			Cart param = new Cart();
			param.setUserId(userId);
			param.setBusinessId(businessId);
			param.setFoodId(foodId);
			int result = cartService.removeCart(param);
			return new CommonResult(200, "success", result);
		}else {
			return new CommonResult(401, "Unauthorized", 0);
		}

	}
}